package com.lanxiao.cloud.modules.dict.controller;

import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.dict.application.DictCmdManager;
import com.lanxiao.cloud.modules.dict.application.DictQueryManager;
import com.lanxiao.cloud.modules.dict.application.dto.DictCmdAdd;
import com.lanxiao.cloud.modules.dict.application.dto.DictCmdUpdate;
import com.lanxiao.cloud.modules.dict.application.dto.DictBaseDto;
import com.lanxiao.cloud.modules.dict.application.dto.DictDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-09-26
 */
@Api(tags = "字典服务")
@Validated
@RestController
@RequestMapping("/dicts/data")
public class DictController {

  private final DictQueryManager dictQueryManager;
  private final DictCmdManager dictCmdManager;

  public DictController(
      DictQueryManager dictQueryManager,
      DictCmdManager dictCmdManager) {
    this.dictQueryManager = dictQueryManager;
    this.dictCmdManager = dictCmdManager;
  }

  @ApiOperation(value = "查询所有字典")
  @GetMapping
  public ResponseEntity<Map<String, List<DictBaseDto>>> queryAll() {
    return ResponseEntity.ok(dictQueryManager.queryAll());
  }

  @ApiOperation(value = "根据类型查询字典")
  @GetMapping("/{type}")
  public ResponseEntity<List<DictDto>> queryType(@PathVariable("type") String type) {
    return ResponseEntity.ok(dictQueryManager.queryType(type));
  }

  @Log(operationType = OperationType.CREATE)
  @ApiOperation(value = "新增字典")
  @PostMapping
  public ResponseEntity<?> save(@Valid @RequestBody DictCmdAdd cmdAdd){
    dictCmdManager.save(cmdAdd);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @Log(operationType = OperationType.UPDATE, serviceClass = DictQueryManager.class)
  @ApiOperation(value = "修改字典")
  @PutMapping("/{id}")
  public ResponseEntity<?> update(@PathVariable Long id, @Valid @RequestBody DictCmdUpdate cmdUpdate){
    dictCmdManager.update(id, cmdUpdate);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @Log(operationType = OperationType.DELETE)
  @ApiOperation(value = "删除字典")
  @DeleteMapping("/{id}")
  public ResponseEntity<?> delete(@PathVariable Long id){
    dictCmdManager.delete(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

}
